package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"services"
	"verification"
)

func AuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		// Parse the json web token.
		context, err := verification.ParseRequest(c)
		if err != nil {
			c.JSON(http.StatusOK, services.Response{
				Code:    200,
				Success: false,
				Message: "无token",
				Data:    nil,
			})

			c.Abort()
			return
		}
		c.Request.Header.Add("id", string(context.ID))
		c.Next()
	}
}
